﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;

namespace BUS
{
    public class CInputKeyShortcut
    {
        string _Control;
        string _Alt;
        string _Shift;
        string _AnyKey;

        public void Install(ref TextBox textBox)
        {
            textBox.KeyDown+=new KeyEventHandler(KeyDown);
            textBox.KeyPress+=new KeyPressEventHandler(KeyPress);
            textBox.KeyUp += new KeyEventHandler(KeyUp);
            textBox.KeyUp += new KeyEventHandler(Clead);
        }

        public void Uninstall(ref TextBox textBox)
        {
            textBox.KeyDown -= new KeyEventHandler(KeyDown);
            textBox.KeyPress -= new KeyPressEventHandler(KeyPress);
            textBox.KeyUp -= new KeyEventHandler(KeyUp);
            textBox.KeyUp -= new KeyEventHandler(Clead);
        }

        public void InstallFind(ref TextBox textBox)
        {
            textBox.KeyDown += new KeyEventHandler(KeyDown);
            textBox.KeyPress += new KeyPressEventHandler(KeyPress);
            textBox.KeyUp += new KeyEventHandler(KeyUp);
        }

        public void UninstallFind(ref TextBox textBox)
        {
            textBox.KeyDown -= new KeyEventHandler(KeyDown);
            textBox.KeyPress -= new KeyPressEventHandler(KeyPress);
            textBox.KeyUp -= new KeyEventHandler(KeyUp);
        }
        private void KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;

            TextBox _txt = (TextBox)sender;

            if (_Control == null && e.Control)
            {
                _Control = CTransferKeysCode.SwithText((int)Keys.ControlKey)+" + ";
            }
            if (_Alt == null && e.Alt)
            {
                _Alt = CTransferKeysCode.SwithText((int)Keys.Menu) + " + ";
            }
            if (_Shift == null && e.Shift)
            {
                _Shift = CTransferKeysCode.SwithText((int)Keys.ShiftKey) + " + ";

            }
            if (e.KeyCode != Keys.ShiftKey && e.KeyCode != Keys.ControlKey && e.KeyCode != Keys.Menu)
            {
                _AnyKey = CTransferKeysCode.SwithText(e.KeyCode);
            }

            _txt.Text = _Control + _Shift + _Alt + _AnyKey;
            _txt.SelectionStart = _txt.TextLength;
        }

        private void KeyUp(object sender, KeyEventArgs e)
        {
            e.Handled = true;

            TextBox _txt = (TextBox)sender;

            if (e.KeyCode == Keys.ControlKey)
            {
                _Control = null;
            }
            else if (e.KeyCode == Keys.ShiftKey)
            {
                _Shift = null;
            }
            else if (e.KeyCode == Keys.Menu)
            {
                _Alt = null;
            }
            else
            {
                _AnyKey = null;
            }
        }

        private void Clead(object sender, KeyEventArgs e)
        {
            TextBox _txt = (TextBox)sender;

            if (_txt.Text.Trim() != null)
            {
                if (_txt.Text[_txt.Text.Length - 1] == ' ')
                {
                    _txt.Text = _Control + _Shift + _Alt + _AnyKey;
                    _txt.SelectionStart = _txt.TextLength;
                }
            }
        }

        private void KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

    }
}
